package com.study.serializable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author huangbaozai
 * @Date 2022/7/25 14:13
 **/
public class DeepCopy {
    public static <T> List<T> copyList(List<T> source) {
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(source);

            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream inStream = new ObjectInputStream(byteIn);
            List<T> list = (List<T>) inStream.readObject();

            inStream.close();
            byteIn.close();
            out.close();
            byteOut.close();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        ArrayList<String> objects = new ArrayList<>();
        objects.add("fe");
        List<String> list = copyList(objects);
        System.out.println(list);

    }
}
